package com.yiren.manager.ctrl.base;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.yiren.core.exception.MyException;
import com.yiren.core.repository.utils.QueryResult;
import com.yiren.core.utils.FileUtil;
import com.yiren.core.utils.HTTPResult;
import com.yiren.core.utils.PropertyConfigurer;
import com.yiren.entity.YrElder;
import com.yiren.entity.YrStaffInfo;
import com.yiren.manager.form.base.YrPersonForm;
import com.yiren.manager.service.iface.department.YrStaffInfoService;
import com.yiren.manager.service.iface.elder.YrElderService;

@RequestMapping("person")
@Controller
public class YrPersonController {

	@Autowired
	private YrElderService elderService;
	
	@Autowired
	private YrStaffInfoService staffInfoService;
	
	@RequestMapping("test")
	@ResponseBody
	public HTTPResult<YrPersonForm> test(String base64) throws MyException{
		HTTPResult<YrPersonForm> result = new HTTPResult<YrPersonForm>();
		
		System.out.println(FileUtil.saveBase64(base64,
				String.valueOf(PropertyConfigurer.getContextProperty("yiren.img.save.url"))));
		return result;
	}
	
	@RequestMapping("findPerson")
	@ResponseBody
	public HTTPResult<YrPersonForm> findPerson(String searchVal) throws MyException{
		HTTPResult<YrPersonForm> result = new HTTPResult<YrPersonForm>();
		String sqlWhere = "name = ?1 OR card_id=?2";
		
		QueryResult<YrElder> qrElder = elderService.find(YrElder.class, sqlWhere, new Object[]{searchVal,searchVal});
		
		List<YrPersonForm> formList  = new ArrayList<YrPersonForm>(); 
		if(qrElder.getTotalRecord()>0){
			for(YrElder elder : qrElder.getResultList()){
				YrPersonForm personForm = new YrPersonForm();
				personForm.setName(elder.getName());
				personForm.setType("长者");
				personForm.setCardId(elder.getCardId());
				personForm.setPersonRelationId(elder.getElderId());
				formList.add(personForm);
			}
		}
		QueryResult<YrStaffInfo> qrStaffInfo = staffInfoService.find(YrStaffInfo.class, sqlWhere, new Object[]{searchVal,searchVal});
		if(qrStaffInfo.getTotalRecord()>0){
			for(YrStaffInfo staff : qrStaffInfo.getResultList()){
				YrPersonForm personForm = new YrPersonForm();
				personForm.setName(staff.getName());
				personForm.setType("职工");
				personForm.setCardId(staff.getCardId());
				personForm.setPersonRelationId(staff.getStaffId());
				formList.add(personForm);
			}
		}
		result.setData(formList);
		return result;
	}
}
